package net.ghsoft.Ctrl
{
	import flash.utils.setTimeout;
	
	import mx.collections.ArrayCollection;
	import mx.events.FlexEvent;
	import mx.graphics.SolidColorStroke;
	
	import spark.components.Group;
	import spark.primitives.Line;
	
	public class MyWait extends Group
	{
		public function MyWait()
		{
			addEventListener(FlexEvent.CREATION_COMPLETE, group_creationCompleteHandler);
			super();
		}
		
		private var _arrHSC:ArrayCollection = new ArrayCollection();
		
		private var _iWait1:int = 2;
		private var _iWait2:int = 1;
		private var _iWait3:int = 0;
		
		protected function group_creationCompleteHandler(event:FlexEvent):void
		{
			if(width >= 4 && height >= 4)
			{
				var xWait:Number = width / 2;
				var yWait:Number = height / 2;

				for(var index:int = 0;index < 12;index ++)
				{
					DrawWait(index * 30, xWait, yWait);
				}
				callLater(RunWait);
			}
		}
		
		private function RunWait():void
		{
			var hsc:SolidColorStroke = null;
			
			for(var index:int = 0; index < _arrHSC.length; index ++)
			{
				hsc = _arrHSC[index];
				hsc.color = 0x000000;
			}
			
			hsc = _arrHSC[_iWait1];
			hsc.color = 0xDDDDDD;
			_iWait1 ++;
			if(_iWait1 >= _arrHSC.length)
			{
				_iWait1 = 0;
			}
			
			hsc = _arrHSC[_iWait2];
			hsc.color = 0xBBBBBB;
			_iWait2 ++;
			if(_iWait2 >= _arrHSC.length)
			{
				_iWait2 = 0;
			}
			
			hsc = _arrHSC[_iWait3];
			hsc.color = 0x999999;
			_iWait3 ++;
			if(_iWait3 >= _arrHSC.length)
			{
				_iWait3 = 0;
			}
			
			
			if(visible)
			{
				setTimeout(RunWait, 300);
			}
		}
		
		private function DrawWait(startFrom:Number, xWait:Number, yWait:Number):void
		{
			startFrom=startFrom*Math.PI/180;
			
			var tmpLeng:Number = (xWait < yWait)?xWait:yWait;
			
			
			var r1:Number = tmpLeng;
			var r2:Number= tmpLeng * 0.6;
			
			var x1:Number = xWait+r1*Math.cos(startFrom);
			var x2:Number = xWait+r2*Math.cos(startFrom);
			
			var y1:Number = yWait + r1*Math.sin(startFrom);
			var y2:Number = yWait + r2*Math.sin(startFrom);
			
			var l1:Line = new Line();
			l1.xFrom = x1;
			l1.xTo = x2;
			
			l1.yFrom = y1;
			l1.yTo = y2
			var hsc:SolidColorStroke = new SolidColorStroke();
			hsc.color = 0x000000;
			hsc.weight = 2;
			l1.stroke = hsc;
			_arrHSC.addItem(hsc);
			addElement(l1);						
		}
	}
}